//
//  THLoadingAnimateView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THLoadingAnimateView.h"

@interface THLoadingAnimateView ()

@property(nonatomic,retain) NSTimer *showTimer;//timer

@property(nonatomic,retain) UIView *bgView;
@property(nonatomic,retain) UIView *centerAnimatedView;
@property(nonatomic,retain) UIView *leftAnimatedView;
@property(nonatomic,retain) UIView *rightAnimatedView;
@property(nonatomic,retain) UILabel*loadingLabel;

@end

@implementation THLoadingAnimateView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
        [self setupUI];
    }
    return self;
}

- (void)commonInit
{
    _offsetY = 8;
    _margin = 10;
    self.fillColor = kThemeColor;
    _dotSize = 8;
}

- (void)setupUI
{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.leftAnimatedView];
    [self.bgView addSubview:self.centerAnimatedView];
    [self.bgView addSubview:self.rightAnimatedView];
    [self.bgView addSubview:self.loadingLabel];
}

- (void)setFillColor:(UIColor *)fillColor
{
    _fillColor = fillColor;
    self.leftAnimatedView.backgroundColor = _fillColor;
    self.centerAnimatedView.backgroundColor = _fillColor;
    self.rightAnimatedView.backgroundColor = _fillColor;
}

- (void)setMasConstraints
{
    self.bgView.frame = self.bounds;
    
    self.centerAnimatedView.layer.cornerRadius = self.dotSize/2;
    self.centerAnimatedView.layer.masksToBounds = YES;
    
    self.leftAnimatedView.layer.cornerRadius = self.dotSize/2;
    self.leftAnimatedView.layer.masksToBounds = YES;
    
    self.rightAnimatedView.layer.cornerRadius = self.dotSize/2;
    self.rightAnimatedView.layer.masksToBounds = YES;
    
    self.centerAnimatedView.frame = CGRectMake((self.frame.size.width - self.dotSize)/2, (self.frame.size.height - self.dotSize)/2, self.dotSize, self.dotSize);
    self.leftAnimatedView.frame   = CGRectMake(self.centerAnimatedView.frame.origin.x - self.margin - self.dotSize, (self.frame.size.height - self.dotSize)/2, self.dotSize, self.dotSize);
    self.rightAnimatedView.frame  = CGRectMake(self.centerAnimatedView.frame.origin.x + self.margin + self.dotSize, (self.frame.size.height - self.dotSize)/2, self.dotSize, self.dotSize);
    self.loadingLabel.frame = CGRectMake(0, self.centerAnimatedView.frame.origin.y  + self.dotSize + 8, self.width, 20);
    
}

//解决当父View释放时，当前视图因为被Timer强引用而不能释放的问题
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (!newSuperview) {
        [self invailTimer];
    }
}

- (void)showInfiniteAnimatedView{
    
    [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.leftAnimatedView.transform = CGAffineTransformTranslate(self.leftAnimatedView.transform, 0,-self.offsetY);
        self.leftAnimatedView.alpha   = 0.2;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.centerAnimatedView.transform = CGAffineTransformTranslate(self.centerAnimatedView.transform, 0,-self.offsetY);
            self.leftAnimatedView.transform  = CGAffineTransformIdentity;
            self.leftAnimatedView.alpha     = 1.0;
            self.centerAnimatedView.alpha   = 0.2;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.rightAnimatedView.transform  = CGAffineTransformTranslate(self.rightAnimatedView.transform, 0,-self.offsetY);
                self.centerAnimatedView.transform = CGAffineTransformIdentity;
                self.centerAnimatedView.alpha = 1.0;
                self.rightAnimatedView.alpha  = 0.2;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    self.rightAnimatedView.transform  =  CGAffineTransformIdentity;
                    self.rightAnimatedView.alpha      = 1.0;
                } completion:^(BOOL finished) {
                    
                }];
            }];
        }];
    }];
}

- (void)startAnimating{
    if (self.alpha == 1.0) {
        return;
    }
    
    self.alpha = 1.0;
    if (self.showTimer) {
        [self invailTimer];
    }
    [self setMasConstraints];
    
    self.showTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showInfiniteAnimatedView) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.showTimer forMode:NSRunLoopCommonModes];
    [self.showTimer fire];
    
}
- (void)stopAnimating{
    [self invailTimer];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showInfiniteAnimatedView) object:nil];
    self.alpha = 0.0;
}
- (void)invailTimer{
    if (_showTimer) {
        [_showTimer invalidate];
        _showTimer = nil;
    }
}

#pragma mark - 懒加载

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
    }
    return _bgView;
}

- (UIView *)leftAnimatedView
{
    if (!_leftAnimatedView) {
        _leftAnimatedView = [[UIView alloc]init];
    }
    return _leftAnimatedView;
}

- (UIView *)centerAnimatedView
{
    if (!_centerAnimatedView) {
        _centerAnimatedView = [[UIView alloc]init];
    }
    return _centerAnimatedView;
}

- (UIView *)rightAnimatedView
{
    if (!_rightAnimatedView) {
        _rightAnimatedView = [[UIView alloc]init];
    }
    return _rightAnimatedView;
}

- (UILabel *)loadingLabel
{
    if(!_loadingLabel)
    {
        _loadingLabel=[[UILabel alloc]init];
        _loadingLabel.textAlignment = NSTextAlignmentCenter;
        _loadingLabel.text = @"加载中";
        _loadingLabel.font = [UIFont systemFontOfSize:10];
        _loadingLabel.textColor = [UIColor whiteColor];
    }
    return _loadingLabel;
}

- (void)dealloc
{
    NSLog(@"空页面加载视图被释放");
}

@end
